package com.xinwei.leetcode.数组;

// https://leetcode-cn.com/problems/container-with-most-water/
public class _11_盛最多水的容器 {
    // 暴力求解  --> 超出时间限制
//    public int maxArea(int[] height) {
//        int max = Integer.MIN_VALUE;
//        for (int i = 0; i < height.length; i++) {
//            for (int j = i + 1; j < height.length; j++) {
//                max = ((j - i) * Math.min(height[i], height[j]) > max) ? ((j - i) * Math.min(height[i], height[j])) : max;
//            }
//        }
//        return max;
//    }


    /**
     * 双指针
     * 先指向两端，哪端小就往中间靠
     * 原理的证明看官解
     */
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int max = 0;
        while (left < right) {
            max = ((right - left) * Math.min(height[left], height[right]) > max) ? ((right - left) * Math.min(height[left], height[right])) : max;
            if (height[right] < height[left]) {
                right --;
            }else {
                left ++;
            }
        }
        return max;
    }

}
